package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Program: health_parent
 * @Description:用户相关操作
 * @Author: choulan
 * @Date:2020-11-17 09:12
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        //这里的user不是pojo的user,而是springsecurity中的
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //分页查询
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findPage(queryPageBean);
    }

    //添加用户
    @PreAuthorize("hasAuthority('USER_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody User user,Integer [] roleIds,HttpSession session){
        try {
            //先对密码进行加密
            BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
            String password = passwordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.add(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    //根据id查询用户信息
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            User user=userService.findById(id);
            return new Result(true,MessageConstant.GET_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USER_FAIL);
        }
    }

    //根据用户id查询角色信息
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer> list=userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.GET_USER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USER_FAIL);
        }
    }

    //编辑用户信息
    @PreAuthorize("hasAuthority('USER_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer [] roleIds){
        try {
            userService.edit(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }

    //删除用户信息
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }
    @RequestMapping("/checkName")
    public Result checkName(String username){
        try {
            boolean isExsist= userService.checkName(username);
            return new Result(true,"校验成功",isExsist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"校验失败");
        }
    }

    //加载用户的菜单信息
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findUserMenus")
    public Result findUserMenus(String username){
        try {
            List<Menu> menus= userService.findUserMenus(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }

    }
}
